package com.qidi.service.impl;

import com.qidi.mapper.OrderDao;
import com.qidi.mapper.OrderItemDao;
import com.qidi.mapper.impl.OrderDaoimpl;
import com.qidi.mapper.impl.OrderItemimplDao;
import com.qidi.pojo.Cart;
import com.qidi.pojo.CartItem;
import com.qidi.pojo.Order;
import com.qidi.pojo.OrderItem;
import com.qidi.service.OrderService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhouXinBin
 * @create 2020-09-04-12:27
 */
public class OrderServiceimpl implements OrderService {
    OrderDao orderDao=new OrderDaoimpl();
    OrderItemDao orderItemDao=new OrderItemimplDao();

    @Override
    public String createOrder(Cart cart, Integer userid) {
        String orderid=System.currentTimeMillis()+"z"+userid;
        int i = orderDao.saveOrder(new Order(orderid, new Date(), cart.getTotalPrice(), 0, userid));
        if (i>0){
            Map<Integer, CartItem> items = cart.getItems();
            for (CartItem item : items.values()) {
                orderItemDao.saveOrderItem(new OrderItem(null,item.getName(),item.getCount(),item.getPrice(),item.getTotalprice(),orderid));

            }
        }
       return orderid;
    }

    @Override
    public void changeOrderStatus(Integer status, String orderid) {
        orderDao.changeOrderStatus(status, orderid);
    }

    @Override
    public List<Order> queryAllOrders() {
        return orderDao.queryAllOrders();
    }

    @Override
    public List<Order> queryMyOrder(Integer userid) {
        return orderDao.queryMyOrder(userid);
    }

    @Override
    public List<OrderItem> queryOrderItems(String orderid) {
        return orderItemDao.queryOrderItems(orderid);
    }
}
